package com.ml.mall.config;

import com.ml.mall.factory.SchedulerJobFactory;
import org.springframework.boot.autoconfigure.quartz.QuartzProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * @author liusongbo
 */
@Configuration
public class QuartzConfiguration {

    private DataSource dataSource;
    private SchedulerJobFactory schedulerJobFactory;
    private QuartzProperties quartzProperties;


    public QuartzConfiguration(DataSource dataSource, SchedulerJobFactory schedulerJobFactory,
                               QuartzProperties quartzProperties) {
        this.dataSource = dataSource;
        this.schedulerJobFactory = schedulerJobFactory;
        this.quartzProperties = quartzProperties;
    }

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean() {
        Properties properties = new Properties();
        properties.putAll(quartzProperties.getProperties());
        SchedulerFactoryBean factory = new SchedulerFactoryBean();
        factory.setOverwriteExistingJobs(true);
        factory.setDataSource(dataSource);
        factory.setQuartzProperties(properties);
        factory.setJobFactory(schedulerJobFactory);
        return factory;
    }
}
